package com.ruoyi.web.controller.tool;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadCutUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.web.controller.common.CommonController;
import com.ruoyi.worker.domain.AttendanceStatisticsImg;
import com.ruoyi.worker.domain.TermWorkerImg;
import com.ruoyi.worker.service.AttendanceGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UploadPictureController
 * @Description:
 * @Author: 刘松山
 * @Date: 2021/9/10 21:09
 */
@RestController
@RequestMapping("/ploadPictureApi")
public class UploadPictureController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private AttendanceGroupService attendanceGroupService;

    @Autowired
    private ServerConfig serverConfig;


    @PostMapping("/addAttendanceImg/{id}")
    public AjaxResult addAttendanceImg(MultipartFile[] file, @PathVariable Long id) throws Exception {

        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();

            String url = "";
            List<AttendanceStatisticsImg> list = new ArrayList<>();
            for (MultipartFile f : file) {
                // 上传并返回新文件名称
                String fileName = FileUploadCutUtils.upload(filePath, f, 1f, 0.8f);
                fileName = fileName.replace("profile", "images");
                url = serverConfig.getUrl() + fileName;

                AttendanceStatisticsImg a = new AttendanceStatisticsImg();
                a.setUrl(url);
                a.setParentId(id);
                a.setCreateBy(getUsername());
                list.add(a);
            }
            AjaxResult ajax = AjaxResult.success();

            ajax.put("url", url);
            attendanceGroupService.addAttendanceStatisticsImg(list);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @Log(title = "上传长期工照片", businessType = BusinessType.OTHER)
    @PostMapping("/addTermWorkerImg")
    public AjaxResult addTermWorkerImg(MultipartFile[] file) throws Exception {

        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();

            String url = "";
            List<TermWorkerImg> list = new ArrayList<>();
            for (MultipartFile f : file) {
                // 上传并返回新文件名称
                String fileName = FileUploadCutUtils.upload(filePath, f, 1f, 0.8f);
                fileName = fileName.replace("profile", "images");
                url = serverConfig.getUrl() + fileName;

            }
            AjaxResult ajax = AjaxResult.success();

            ajax.put("url", url);

            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

}
